/*********************************************************************************/
/*                                                                               */
/*      ADC Driver                                                               */
/*                                                                               */
/*      Last updated by:  CBS                                                    */
/*      Last update date: 12/12/25                                               */
/*      Revision:         0                                                      */
/*      Copyright:        DENSO                                                  */
/*                                                                               */
/*********************************************************************************/

#ifndef ADC_H
#define ADC_H

/*********************************************************************************/
/*  include files                                                                */
/*********************************************************************************/
#include "Std_Types.h"
#include "Adc_Cfg.h"
#include "Dma.h"

/*********************************************************************************/
/*  defines / data types / structs / unions / macros                             */
/*********************************************************************************/
#define ADC_VENDOR_ID        (U2)1
#define MODULE_ID_ADC        (U2)123
#define ADC_INSTANCE_ID      (U1)0

#define ADC_SW_MAJOR_VERSION (U1)1
#define ADC_SW_MINOR_VERSION (U1)0
#define ADC_SW_PATCH_VERSION (U1)0

#define ADC_AR_MAJOR_VERSION (U1)1
#define ADC_AR_MINOR_VERSION (U1)0
#define ADC_AR_PATCH_VERSION (U1)0

typedef U2 Adc_EQADCRegister;

typedef enum
{
    ADC_GROUP0 = (U1)0,
    ADC_GROUP1 = (U1)1,
    ADC_GROUP2 = (U1)2,
    ADC_GROUP3 = (U1)3,
    ADC_GROUP4 = (U1)4,
    ADC_GROUP5 = (U1)5,
    ADC_NBR_OF_GROUPS = (U1)6
}Adc_GroupType;

/* API service ID */
typedef enum
{
    ADC_INIT_ID = 0x00,
    ADC_DEINIT_ID = 0x01,
    ADC_STARTGROUPCONVERSION_ID = 0x02,
    ADC_STOPGROUPCONVERSION_ID = 0x03,
    ADC_READGROUP_ID = 0x04,
    ADC_ENABLEHARDWARETRIGGER_ID = 0x05,
    ADC_DISABLEHARDWARETRIGGER_ID = 0x06,
    ADC_ENABLEGROUPNOTIFICATION_ID = 0x07,
    ADC_DISABLEGROUPNOTIFICATION_ID = 0x08,
    ADC_GETGROUPSTATUS_ID = 0x09,
    ADC_GETVERSIONINFO_ID = 0x0A,
    ADC_GETSTREAMLASTPOINTER_ID = 0x0B,
    ADC_SETUPRESULTBUFFER_ID = 0x0C
}Adc_APIServiceIDType;

/* DET errors that the ADC can produce */
typedef enum
{
    ADC_E_UNINIT              = 0x0A,
    ADC_E_BUSY                = 0x0B,
    ADC_E_IDLE                = 0x0C,
    ADC_E_ALREADY_INITIALIZED = 0x0D,
    ADC_E_PARAM_CONFIG        = 0x0E,
    ADC_E_PARAM_POINTER       = 0x14,
    ADC_E_PARAM_GROUP         = 0x15,
    ADC_E_WRONG_CONV_MODE     = 0x16,
    ADC_E_WRONG_TRIGG_SRC     = 0x17,
    ADC_E_NOTIF_CAPABILITY    = 0x18,
    ADC_E_BUFFER_UNINIT       = 0x19
}Adc_DetErrorType;

typedef struct
{
    Adc_GroupAccessModeType      accessMode;
    Adc_GroupConvModeType        conversionMode;
    Adc_TriggerSourceType        triggerSrc;
    Adc_HwTriggerSignalType      hwTriggerSignal;
    Adc_HwTriggerTimerType       hwTriggerTimer;
    void                         (*groupCallback)(void);
    Adc_StreamBufferModeType     streamBufferMode;
    Adc_StreamNumSampleType      streamNumSamples;
    const Adc_ChannelType        *channelList;
    Adc_ValueGroupType           *resultBuffer;
    Adc_CommandType              *commandBuffer;       
    U1                           numberOfChannels;
    Adc_GroupStatus              *status;
    Dma_ChannelType              dmaCommandChannel;
    Dma_ChannelType              dmaResultChannel;
    const struct tcd_t           *groupDMACommands;
    const struct tcd_t           *groupDMAResults;
}Adc_GroupDefType;

typedef struct
{
    const Adc_HWConfigurationType*         hwConfigPtr;
    const Adc_ChannelConfigurationType*    channelConfigPtr;
    const U2                               nbrOfChannels;
    const Adc_GroupDefType*                groupConfigPtr;
    const U2                               nbrOfGroups;
}Adc_ConfigType;

typedef enum
{
  ADC0_CR = 1,
  ADC0_TSCR,
  ADC0_TBCR,
  ADC0_GCCR,
  ADC0_OCCR
}Adc_EQADCRegisterType;

/********************************************************************************/
/*  API Functions                                                               */
/********************************************************************************/

/************************************************************************************************************************/
/*  Service name        :   Adc_Init                                                                                    */
/*  Sync/Async          :   Synchronous                                                                                 */
/*  Reentrancy          :   Non Reentrant                                                                               */
/*  Parameters (in)     :   Pointer to configuration set in Variant PB(Variant PC requires a NULL_PTR)                  */
/*  Return value        :   None                                                                                        */
/*  Discription         :   Initializes the ADC hardware units and driver                                               */
/************************************************************************************************************************/
void Adc_Init(const Adc_ConfigType *ConfigPtr);

/************************************************************************************************************************/
/*  Service name        :    Adc_DeInit                                                                                 */
/*  Sync/Async          :    Synchronous                                                                                */
/*  Reentrancy          :    Non Reentrant                                                                              */
/*  Parameters          :    none                                                                                       */
/*  Return value        :    none                                                                                       */
/*  Discription         :    Returns all ADC HW Units to a state comparable to their power on reset state.              */
/************************************************************************************************************************/
#if (ADC_DEINIT_API == STD_ON)  /* ADC228 */
void Adc_DeInit (void);
#endif  /* ADC_DEINIT_API */

/************************************************************************************************************************/
/*  Service name        :    Adc_SetupResultBuffer                                                                      */
/*  Sync/Async          :    Synchronous                                                                                */
/*  Reentrancy          :    Re-entrant                                                                                 */
/*  Parameters (in)     :    Group             Numeric ID of requested ADC channel group.                               */
/*                           DataBufferPtr     pointer to result data buffer                                            */
/*  Return value        :    Std_ReturnType    E_OK: result buffer pointer initialized correctly                        */
/*                                             E_NOT_OK: operation failed or development error occured                  */
/*  Discription         :    Initializes ADC driver with the group specific result buffer start address where the       */
/*                           conversion results will be stored. The application has to ensure that the application      */
/*                           buffer,where DataBufferPtr points to, can hold all the conversion results of the           */
/*                           specified group.The initialization with Adc_SetupResultBuffer is required after reset,     */
/*                           before a group conversion can be started.                                                  */
/************************************************************************************************************************/
Std_ReturnType Adc_SetupResultBuffer(Adc_GroupType Group, Adc_ValueGroupType *BufferPtr);

/************************************************************************************************************************/
/*  Service name        :    Adc_StartGroupConversion                                                                   */
/*  Sync/Async          :    ASynchronous                                                                               */
/*  Reentrancy          :    Re-entrant                                                                                 */
/*  Parameters (in)     :    Group         Numeric ID of requested ADC Channel group.                                   */
/*  Return value        :    none                                                                                       */
/*  Description         :    Starts the conversion of all channels of the requested ADC Channel group.                  */
/************************************************************************************************************************/
#if (ADC_ENABLE_START_STOP_GROUP_API == STD_ON)  /* ADC259 */
void Adc_StartGroupConversion (Adc_GroupType Group);
#endif  /* ADC_ENABLE_START_STOP_GROUP_API */

/************************************************************************************************************************/
/*  Service name        :    Adc_StopGroupConversion                                                                    */
/*  Sync/Async          :    Synchronous                                                                                */
/*  Reentrancy          :    Re-entrant                                                                                 */
/*  Parameters (in)     :    Group         Numeric ID of requested ADC Channel group.                                   */
/*  Return value        :    none                                                                                       */
/*  Description         :    Stops the conversion of the requested ADC Channel group.                                   */
/************************************************************************************************************************/
#if (ADC_ENABLE_START_STOP_GROUP_API == STD_ON)  /* ADC260 */
void Adc_StopGroupConversion (Adc_GroupType Group);
#endif  /* ADC_ENABLE_START_STOP_GROUP_API */

/************************************************************************************************************************/
/*  Service name        :    Adc_ReadGroup                                                                              */
/*  Sync/Async          :    Synchronous                                                                                */
/*  Reentrancy          :    Re-entrant                                                                                 */
/*  Parameters (in)     :    Group             Numeric ID of requested ADC Channel group.                               */
/*                           DataBufferPtr     ADC results of all channels of the selected group are stored             */
/*                                             in the data buffer addressed with the pointer.                           */
/*  Return value        :    Std_ReturnType    E_OK: results are available and written to the data buffer               */
/*                                             E_NOT_OK: no results are available or development error occured          */
/*  Description         :    Reads the group conversion result of the last completed conversion round of the            */
/*                           requested group and stores the channel values starting at the DataBufferPtr                */
/*                           address. The group channel values are stored in ascending channel number order             */
/*                           (in contrast to the storage layout of the result buffer if streaming access is configured).*/
/************************************************************************************************************************/
#if (ADC_READ_GROUP_API == STD_ON)  /* ADC359 */
Std_ReturnType Adc_ReadGroup(Adc_GroupType Group, Adc_ValueGroupType *DataBufferPtr);
#endif  /* ADC_READ_GROUP_API */

/************************************************************************************************************************/
/*  Service name        :    Adc_EnableHardwareTrigger                                                                  */
/*  Sync/Async          :    Synchronous                                                                                */
/*  Reentrancy          :    Re-entrant                                                                                 */
/*  Parameters (in)     :    Group         Numeric ID of requested ADC Channel group.                                   */
/*  Return value        :    none                                                                                       */
/*  Description         :    Enables the hardware trigger for the requested ADC Channel group.                          */
/************************************************************************************************************************/
#if (ADC_HW_TRIGGER_API == STD_ON)  /* ADC265 */
void  Adc_EnableHardwareTrigger(Adc_GroupType group);
#endif  /* ADC_HW_TRIGGER_API */

/************************************************************************************************************************/
/*  Service name        :    Adc_DisableHardwareTrigger                                                                 */
/*  Sync/Async          :    Synchronous                                                                                */
/*  Reentrancy          :    Re-entrant                                                                                 */
/*  Parameters (in)     :    Group         Numeric ID of requested ADC Channel group.                                   */
/*  Return value        :    none                                                                                       */
/*  Description         :    Disables the hardware trigger for the requested ADC Channel group.                         */
/************************************************************************************************************************/
#if (ADC_HW_TRIGGER_API == STD_ON)  /* ADC266 */
void Adc_DisableHardwareTrigger(Adc_GroupType group);
#endif  /* ADC_HW_TRIGGER_API */

/************************************************************************************************************************/
/*  Service name        :    Adc_EnableGroupNotification                                                                */
/*  Sync/Async.         :    Synchronous                                                                                */
/*  Reentrancy          :    Re-entrant                                                                                 */
/*  Parameters (in)     :    Group         Numeric ID of requested ADC Channel group.                                   */
/*  Return value        :    none                                                                                       */
/*  Description         :    Enables the notification mechanism for the requested ADC Channel group.                    */
/************************************************************************************************************************/
#if (ADC_GRP_NOTIF_CAPABILITY == STD_ON)  /* ADC100 */
void Adc_EnableGroupNotification (Adc_GroupType group);
#endif  /* ADC_GRP_NOTIF_CAPABILITY */

/************************************************************************************************************************/
/*  Service name        :    Adc_DisableGroupNotification                                                               */
/*  Sync/Async          :    Synchronous                                                                                */
/*  Reentrancy          :    Re-entrant                                                                                 */
/*  Parameters (in)     :    Group         Numeric ID of requested ADC Channel group.                                   */
/*  Return value        :    none                                                                                       */
/*  Description         :    Disables the notification mechanism for the requested ADC Channel group.                   */
/************************************************************************************************************************/
#if (ADC_GRP_NOTIF_CAPABILITY == STD_ON)  /* ADC101 */
void Adc_DisableGroupNotification (Adc_GroupType group);
#endif  /* ADC_GRP_NOTIF_CAPABILITY */

/************************************************************************************************************************/
/*  Service name        :    Adc_GetGroupStatus                                                                         */
/*  Sync/Async          :    Synchronous                                                                                */
/*  Reentrancy          :    Re-entrant                                                                                 */
/*  Parameters (in)     :    Group            Numeric ID of requested ADC Channel group.                                */
/*  Return value        :    Adc_StatusType    Conversion status for the requested group.                               */
/*  Description         :    Returns the conversion status of the requested ADC Channel group.                          */
/************************************************************************************************************************/
Adc_StatusType Adc_GetGroupStatus (Adc_GroupType group);

/************************************************************************************************************************/
/*  Service name        :    Adc_GetStreamLastPointer                                                                   */
/*  Sync/Async          :    Synchronous                                                                                */
/*  Reentrancy          :    Re-entrant                                                                                 */
/*  Parameters (in)     :    Group               Numeric ID of requested ADC Channel group.                             */
/*  Parameters (out)    :    PtrToSamplePtr      Pointer to result buffer pointer.                                      */
/*  Return value        :    Adc_StreamNumSampleType     Number of valid samples per channel.                           */
/*  Description         :    Returns the number of valid samples per channel, stored in the result buffer.              */
/*                           Reads a pointer, pointing to a position in the group result buffer. With the pointer       */
/*                           position, the results of all group channels of the last completed conversion round         */
/*                           can be accessed.With the pointer and the return value, all valid group conversion results  */
/*                           can be accessed (the user has to take the layout of the result buffer into account).       */
/************************************************************************************************************************/
Adc_StreamNumSampleType  Adc_GetStreamLastPointer(Adc_GroupType group,Adc_ValueGroupType** PtrToSamplePtr);

/************************************************************************************************************************/
/*  Service name        :    Adc_GetVersionInfo                                                                         */
/*  Sync/Async.         :    Synchronous                                                                                */
/*  Reentrancy          :    Re-entrant                                                                                 */
/*  Parameters (in)     :    versioninfo         Pointer to where to store the version information of this module.      */
/*  Return value        :    none                                                                                       */
/*  Description         :    Returns the version information of this module.                                            */
/************************************************************************************************************************/
#if(ADC_VERSION_INFO_API == STD_ON)        /* ADC237 */
void  Adc_GetVersionInfo(Std_VersionInfoType* Versioninfo);
#endif  /* ADC_DEINIT_API */

#endif  /* ADC_H */
/**** End Of File ****************************************************************/
